package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.testapplication.JetPact.StarViewModel;
import com.example.testapplication.bean.Star;

public class xueJetpackViewModel extends AppCompatActivity {

    private StarViewModel starViewModel;
    private TextView starTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xue_jetpack_view_model);

        starTv = findViewById(R.id.starTv);

//        1.拿到了ViewModel
        starViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(StarViewModel.class);
//        2.初始化数据
        starViewModel.getMutableLiveData().postValue(new Star("金城武",1000));

        starViewModel.getMutableLiveData().observe(this, new Observer<Star>() {
            @Override
            public void onChanged(Star star) {
                starTv.setText(star.getName() + "粉丝:" + star.getFans());
            }
        });
    }

    public void doAddFans(View view) {
        starViewModel.addFans();
    }
}
